﻿using Orleans.Configuration;
using Orleans;
using Orleans.Hosting;
using Microsoft.Extensions.Logging;
using DeviceClientGrain;
using System;

namespace DeviceDemo
{
    internal class Program
    {
        static async  Task Main(string[] args)
        {
            Console.WriteLine("开始启动机台管理端");
            try
            {
                using (var client = await ConnectClient())
                { 
                    var 服务 = client.GetStreamProvider("DeviceProvider");
                    var stream = 服务.GetStream<MsgData>(Guid.Empty, "AGV");
                    await stream.SubscribeAsync(new DeviceMsgObserver());
                     
                    await client.Close();
                } 
            }
            catch (Exception e)
            {
                Console.WriteLine($"\nException 异常: {e.Message}"); 
                Console.ReadKey(); 
            }

            Console.WriteLine("程序结束");
        }



        static async Task<IClusterClient> ConnectClient()
        {
            IClusterClient client;
            client = new ClientBuilder() 
                 .UseLocalhostClustering()
                .AddSimpleMessageStreamProvider("DeviceProvider")
                .ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Error).AddConsole())
                .Build();

            await client.Connect();
            Console.WriteLine("Client successfully connected to host \n");
            return client;
        }
    }
}